package game

import (
	"fmt"
	"server/csvs"
	"time"
)

type ShowRole struct {
	RoleId    int
	RoleLevel int
}

type ModPlayer struct {
	UserId         int    // 唯一id
	Icon           int    // 头像
	Card           int    // 名片
	Name           string // 用户名
	Sign           string // 签名
	PlayerLevel    int    // 玩家等级
	PlayerExp      int    // 玩家经验
	WorldLevel     int    // 实际大世界等级
	WorldLevelNow  int    // 当前大世界的等级
	WorldLevelCool int64
	Birth          int
	ShowTeam       []*ShowRole // 展示角色的信息
	HidenShowTeam  int         // 是否隐藏角色
	ShowCard       []int       // 展示的名片
	//	看不见的字段
	IsProhibit int // 是否被封号
	IsGM       int // 是否是测试号

}

func (self *ModPlayer) SetIcon(player *Player, iconId int) {
	if !player.ModIcon.IsHasIcon(iconId) {
		// 通知客户端该用户操作非法
		return
	}

	player.ModPlayer.Icon = iconId
	fmt.Println("当前图标: ", player.ModPlayer.Icon)
}

func (self *ModPlayer) SetCard(player *Player, card int) {
	if !player.ModCard.IsHasCard(card) {
		// 通知客户端该用户操作非法
		return
	}

	player.ModPlayer.Card = card
	fmt.Println("当前名片: ", player.ModPlayer.Card)
}

func (self *ModPlayer) SetName(player *Player, name string) {
	if GetManageBanWord().IsBanWord(name) {
		return
	}
	player.ModPlayer.Name = name
	fmt.Println("当前名称: ", player.ModPlayer.Name)
}

func (self *ModPlayer) SetSign(player *Player, sign string) {
	if GetManageBanWord().IsBanWord(sign) {
		return
	}

	player.ModPlayer.Sign = sign
	fmt.Println("当前签名: ", player.ModPlayer.Sign)
}

func (self *ModPlayer) AddExp(player *Player, exp int) {
	// 是否会溢出 todo
	self.PlayerExp += exp

	for {
		config := csvs.GetNowLevelConfig(self.PlayerLevel)
		if config == nil {
			break
		}
		// 满级了
		if config.PlayerExp == 0 {
			break
		}
		// 是否完成等级突破挑战任务(大于0说明要做升级任务)
		if config.ChapterId > 0 && !player.ModUniqueTask.IsTaskFinished(config.ChapterId) {
			break
		}

		if self.PlayerExp >= config.PlayerExp {
			self.PlayerLevel += 1
			self.PlayerExp -= config.PlayerExp
		} else {
			break
		}
	}
	fmt.Println("当前等级: ", self.PlayerLevel, "当前经验: ", self.PlayerExp)
}

func (self *ModPlayer) ReduceWorldLevel(player *Player) {
	if self.WorldLevel < csvs.REDUCE_WORLD_LEVEL_START {
		fmt.Println("操作失败:", "当前世界等级: ", self.WorldLevelNow, "真实世界等级: ", self.WorldLevel)
		return
	}

	// 已经降低过世界等级了
	if self.WorldLevelNow != self.WorldLevel {
		fmt.Println("操作失败:", "当前世界等级: ", self.WorldLevelNow, "真实世界等级: ", self.WorldLevel)
		return
	}

	self.WorldLevelNow = self.WorldLevel - csvs.REDUCE_WORLD_LEVEL_MAX
	// 世界等级可以恢复的时间戳限制
	self.WorldLevelCool = time.Now().Unix() + csvs.REDUCE_WORLD_LEVEL_COOL_TIME
	fmt.Println("当前世界等级: ", self.WorldLevelNow, "真实世界等级: ", self.WorldLevel)
}

func (self *ModPlayer) ReturnWorldLevel(player *Player) {
	// 世界等级还没有降低过
	if self.WorldLevelNow == self.WorldLevel {
		fmt.Println("操作失败:", "当前世界等级: ", self.WorldLevelNow, "真实世界等级: ", self.WorldLevel)
		return
	}

	// 恢复等级的时间还没有到
	if self.WorldLevelCool > time.Now().Unix() {
		fmt.Println("世界等级恢复还在冷却中")
		return
	}

	// 恢复真实的世界等级
	self.WorldLevelNow = self.WorldLevel
}

func (self *ModPlayer) SetBirth(player *Player, birth int) {
	if self.Birth != 0 {
		fmt.Println("已经设置过生日了")
		return
	}
	month := birth / 100
	day := birth % 100
	switch month {
	case 1, 3, 5, 7, 8, 10, 12:
		if day < 0 || day > 31 {
			fmt.Println("大月天数没有", day, "天")
			return
		}
	case 2:
		if day < 0 || day > 29 {
			fmt.Println("2月天数没有", day, "天")
			return
		}
	case 4, 6, 9, 11:
		if day < 0 || day > 30 {
			fmt.Println("小月天数没有", day, "天")
			return
		}
	default:
		fmt.Println("没有", month, "月")
		return
	}
	self.Birth = birth
	fmt.Println("设置生日成功: ", month, "月", day, "日")

	if self.IsBirthday() {
		fmt.Println("旅行者, 生日快乐!")
	} else {
		fmt.Println("期待你的生日!")
	}
}

func (self *ModPlayer) IsBirthday() bool {
	month := time.Now().Month()
	day := time.Now().Day()

	if int(month) == (self.Birth/100) && day == (self.Birth%100) {
		return true
	} else {
		return false
	}
}

func (self *ModPlayer) SetShowCards(player *Player, cardsId []int) {
	if len(cardsId) > csvs.SHOW_SIZE {
		fmt.Println("客户端设置错误")
		return
	}
	cardsExist := make(map[int]int)
	// cardsIdNew存在的目的是为了保持cardsId中id的顺序
	cardsIdNew := make([]int, 0)

	for _, cardId := range cardsId {
		if _, ok := cardsExist[cardId]; ok {
			continue
		}
		if !player.ModCard.IsHasCard(cardId) {
			continue
		}

		cardsIdNew = append(cardsIdNew, cardId)
		cardsExist[cardId] = 1
	}

	self.ShowCard = cardsIdNew
	fmt.Println("设置展示名片为： ", self.ShowCard)
}

func (self *ModPlayer) SetShowTeam(player *Player, showRolesId []int)  {
	if len(showRolesId) > csvs.SHOW_SIZE {
		fmt.Println("客户端设置错误")
		return
	}
	rolesExist := make(map[int]int)
	// cardsIdNew存在的目的是为了保持cardsId中id的顺序
	rolesIdNew := make([]*ShowRole, 0)

	for _, roleId := range showRolesId {
		if _, ok := rolesExist[roleId]; ok {
			continue
		}
		if !player.ModRole.IsHasRole(roleId) {
			continue
		}

		var showRole = &ShowRole{
			RoleId: roleId,
			RoleLevel: player.ModRole.GetRoleLevel(roleId),
		}

		rolesIdNew = append(rolesIdNew, showRole)
		rolesExist[roleId] = 1
	}

	self.ShowTeam = rolesIdNew
}

func (self *ModPlayer) SetHidenShowTeam(player *Player, isHiden int)  {
	if isHiden > 0 {
		self.HidenShowTeam = csvs.LOGIC_TRUE
	}else{
		self.HidenShowTeam = csvs.LOGIC_FALSE
	}
}